﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MKAdminCore.DTO.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MKAdminCore.ToolKit.Extensions
{
    /// <summary>
    /// 获取配置文件信息
    /// </summary>
    public static class AppSettingConfigExtensions
    {
        public static void InitConfig(this IServiceCollection services, IConfiguration configuration)
        {
            //连接字符串
            string readConnStr = configuration.GetConnectionString("ReadConnStr");

            AppSettingsJsonInfo.ConnectionStrings = new ConnectionStringsInfo();
            AppSettingsJsonInfo.ConnectionStrings.ReadConnStr = readConnStr;

            string hostConnection = configuration.GetSection("RedisDbConfig:HostConnection").Value;
            int dataBaseIndex = Convert.ToInt32(configuration.GetSection("RedisDbConfig:DataBaseIndex").Value);
            AppSettingsJsonInfo.RedisConfig = new RedisDbConfig();
            AppSettingsJsonInfo.RedisConfig.HostConnection = hostConnection;
            AppSettingsJsonInfo.RedisConfig.DataBaseIndex = dataBaseIndex;

            //文件目录配置节点
            //获取节点个数
            int uploadFileCount = configuration.GetSection("UploadFile").GetChildren().Count();

            AppSettingsJsonInfo.UploadFileList = new List<UploadFileInfo>();
            for (int i = 0; i < uploadFileCount; i++)
            {
                string key = configuration.GetSection($"UploadFile:{i}:Key").Value;
                string FileSize = configuration.GetSection($"UploadFile:{i}:FileSize").Value;
                string FileTypeLimit = configuration.GetSection($"UploadFile:{i}:FileTypeLimit").Value;
                string SavePath = configuration.GetSection($"UploadFile:{i}:SavePath").Value;
                string SaveUrl = configuration.GetSection($"UploadFile:{i}:SaveUrl").Value;

                AppSettingsJsonInfo.UploadFileList.Add(new UploadFileInfo()
                {
                    Key = key,
                    FileSize = Convert.ToInt32(FileSize),
                    FileTypeLimit = FileTypeLimit,
                    SavePath = SavePath,
                    SaveUrl = SaveUrl
                });
            }

            //邮件配置信息
            AppSettingsJsonInfo.EmailInfo = new EmailConfigInfo()
            {
                Enabled = Convert.ToBoolean(configuration.GetSection($"EmailConfig:Enabled").Value),
                Host = configuration.GetSection($"EmailConfig:Host").Value,
                Port = Convert.ToInt32(configuration.GetSection($"EmailConfig:Port").Value),
                UserName = configuration.GetSection($"EmailConfig:UserName").Value,
                UserAddress = configuration.GetSection($"EmailConfig:UserAddress").Value,
                Password = configuration.GetSection($"EmailConfig:Password").Value
            };
        }

        /// <summary>
        /// 获取上传配置文件信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static UploadFileInfo GetUploadFileConfigInfo(string key)
        {
            var uploadFileList = AppSettingsJsonInfo.UploadFileList;
            if (uploadFileList == null)
            {
                throw new Exception("获取上传配置节点失败");
            }

            var uploadFileInfo = uploadFileList.Where(q => q.Key == key).FirstOrDefault();
            if (uploadFileInfo == null)
            {
                throw new Exception($"{key}未配置上传节点");
            }

            return uploadFileInfo;
        }

    }
}
